/*********** NOTES HEADER *********** 

DESCRIPTION: Merges the matched sample identifiers onto the BENE-QTR information.  

Datasets Used:
	(1) PSPats_logit_final
	(2) Bene_matchdata_qtr_2005_2012
	(3) DR_BENE_CHARS_QTR_2005_2012

Datasets Created:
	(1) Bene_matchdata_qtr_dups
	(2) PSPats_logit_final
	(3) PS-logit-BENE-QUARTER_FINAL2005-2012
	 
************************************/ 

set more off
capture log close
clear all
global origData "N:\MedicareClaims-P045601-BE"
global dataIn "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-In\"
global dataOut "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-Out"
global dpath "N:\MedicareClaims-P045601-BE\Work\ay_data"
global dataProp "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-Out/PropScore/Patients"
global logs "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Logs\PropScore\Patients"
global dpath "N:\MedicareClaims-P045601-BE\Work\ay_data"
global skapath "N:\MedicareClaims-P045601-BE\Work\ska"

adopath +  N:\SIL-Common\estout
adopath +  N:\SIL-Common\outreg2
adopath +  N:\SIL-Common\reghdfe-master/package

*This is the list of propensity score patients generated in the previous program
 use "$dataOut/PSPats_logit_final.dta"
	capture rename merger merger_seen
	*preparing to merge onto analysis dataset
	sort BENE_ID
 save, replace
 clear

*Getting a quarterly measure of the number of doctors visited
*Main dataset
	use "$dataOut/Bene_matchdata_qtr_2005_2012.dta"  
	 sort BENE_ID 
	 drop PHYS_Z*
	 duplicates tag BENE_ID quarter, gen(tag)
	 keep if tag>0
	save "$dataOut/Bene_matchdata_qtr_dups.dta", replace

	collapse (max) PSPEC* q_of_merger DEAD age mx* new* firmct RACE* SEX* merger hosp* hs* pop* urban*, by(BENE* quarter vm* STATE* TAX* docid )
	 duplicates tag BENE_ID quarter, gen(tag)
	 sort BENE_ID quarter BENE_ZIP 
	 by BENE_ID quarter: gen ctr=_n
	 drop if tag>0 & ctr>1
	 drop ctr tag
	save "$dataOut/Bene_matchdata_qtr_dups.dta", replace

	use "$dataOut/Bene_matchdata_qtr_2005_2012.dta"  
	 sort BENE_ID 
	 drop PHYS_Z*
	 duplicates tag BENE_ID quarter, gen(tag)
	 drop if tag>0
	 drop tag
	append using "$dataOut/Bene_matchdata_qtr_dups.dta"
	rename merger mg

	*merge propensity score data to analysis dataset
	*Note that this looks like only _merge==2 and _merge==3; no _merge==1
	merge m:1 BENE_ID using "$dataOut/PSPats_logit_final.dta"
		rename merger merger_seen
		rename mg merger
		tab _merge
		keep if _merge==3
		drop if quarter==.
		drop _merge

*Specialty
		foreach i of varlist PSPEC* {
			local j=regexr("`i'","PSPEC","")
			di "`j'"
			rename `i' psps`j'
		}

		drop PSTATE* birthage qtr sex_pat race_pat
		rename id propScoreId

*Family practice
	gen fmp=psps08
	 replace fmp=1 if psps01==1 
	 replace fmp=1 if psps11==1
 	 replace fmp=1 if psps93==1
	egen SPEC=group(fmp psps02 psps04 psps05 psps06 psps07 psps10 psps13 psps16 psps18 psps20 psps22 psps25 psps26 psps29 psps30 psps34 psps39 psps41 psps48 psps83  )

duplicates drop
sort BENE_I quarter
merge 1:1  BENE_I quarter using "$dataProp\DR_BENE_CHARS_QTR_2005_2012.dta"
drop if _merge==2
drop _merge 


*Vertical mergers [begin]
	preserve 
		tempfile vmfile
		keep BENE_ID quarter vm*
		reshape long vm_PROVIDER, i(BENE_ID quarter) j(counter)
		drop if vm_PROVIDER==.
		drop counter
		duplicates drop
		sort BENE_ID quarter vm_PROVIDER
		by BENE_ID quarter : gen ctr=_n
		reshape wide vm_PROVIDER, i(BENE_ID quarter) j(ctr)
		sort BENE_ID quarter
		save `vmfile', replace
*Vertical mergers [End]
	restore

		sort BENE_ID quarter
		drop vm*
		merge 1:1 BENE_ID quarter using `vmfile'
	 drop _merge merger
	save "$dataProp/PS-logit-BENE-QUARTER_FINAL2005-2012.dta", replace

 
